import math
import unittest
import itertools
import operator

def ispalindrome(n):
	'''check if n is palidrome. no string control or pre-processing (for non digits character).
	'''
	n = str(n)
	for i in range(len(n)):
		if n[i]!=n[-(i+1)]: return False
	return True

def ndigits(n):
	'''generator for n digits number. no negative, no floating points
	'''
	n = abs(math.trunc(n))
	i = 10**(n-1)
	while i<10**n:
		yield i
		i += 1

class TestAll(unittest.TestCase):
	def test_palindrome(self):
		good = ["", 1, 11, 121, 9009, 909]
		bad = [12, 123, 1234, 9019, 1201]
		
		for i in good: self.assertTrue(ispalindrome(i))
		for i in bad: self.assertFalse(ispalindrome(i))

	def test_generator(self):
		f = lambda x: len(str(x))
		for k in range(6):
			itr = ndigits(k)
			for i in itr:
				self.assertTrue(f(k))

	def test_examples(self):
		'''this solve the challange in iterative way'''
		a = list(ndigits(3))
		b = list(ndigits(3))
		pals = []
		for ia in a:
			for ib in b:
				r = ia * ib
				if ispalindrome(r):
					pals.append(r)
		print(max(pals))
	
	def test_example_combinatorics(self):
		'''this solve the challange in functional way'''
		a = list(ndigits(3))
		mul_comb = map(lambda x:x[0]*x[1], itertools.permutations(a, 2))
		mul_comb = filter(ispalindrome, mul_comb)
		print(max(mul_comb))

if __name__ == "__main__":
	unittest.main()
